package com.loveyukari.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.loveyukari.dao.AuthDao;
import com.loveyukari.model.Auth;

/**
*
*yangshaohua
*2017年1月23日下午7:54:39
**/
@Repository
public class JdbcAuthDao extends BaseDao implements AuthDao{

	private class AuthRowMapper implements RowMapper<Auth>{

		public Auth mapRow(ResultSet rs, int rowNum) throws SQLException {
			Auth auth = new Auth();
			auth.setId(rs.getInt("id"));
			auth.setName(rs.getString("name"));
			auth.setLoginTime(rs.getTimestamp("login_time"));
			auth.setAuthority(rs.getInt("authority"));
			auth.setNickName(rs.getString("nick_name"));
			auth.setIcon(rs.getString("icon"));
			auth.setLv(rs.getInt("lv"));
			auth.setFansLv(rs.getInt("fans_lv"));
			auth.setGuizu(rs.getInt("guizu"));
			auth.setScore(rs.getInt("score"));
			return auth;
		}
		
	}
	

	public Auth selectByUsername(String username) {
		String sql = "select * from auth where username = ?";
		List<Auth> list = this.getJdbcTemplate().query(sql, new AuthRowMapper(),username);
		if (list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}

	public void insertAuthSign(int authId) {
		String sql = "insert into auth_sign (auth_id,sign_date) values(?,now())";
		this.getJdbcTemplate().update(sql,authId);
	}

	public void insertAuth(Auth auth) {
		String sql = "insert into auth (id,name,authority,icon,lv,fans_lv) values(?,?,?,?,?,?)";
		this.getJdbcTemplate().update(sql,auth.getId(),auth.getName(),auth.getAuthority(),auth.getIcon(),auth.getLv(),auth.getFansLv());
	}

	public List<Date> selectAuthSignDate(int authId) {
		String sql = "select * from auth_sign where auth_id = ? order by sign_date desc";
		List<Date> list = this.getJdbcTemplate().query(sql, new RowMapper<Date>(){
			public Date mapRow(ResultSet rs, int rowNum) throws SQLException {
				return rs.getDate("sign_date");
			}
			
		},authId);
		return list;
	}

	public Auth selectById(int id) {
		String sql = "select * from auth where id = ?";
		List<Auth> list = this.getJdbcTemplate().query(sql, new AuthRowMapper(),id);
		if (list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}

	public void updateAuthName(Auth auth) {
		String sql = "update auth set name=? where id = ?";
		this.getJdbcTemplate().update(sql, auth.getName(),auth.getId());
	}


	public void updateAuthIcon(Auth auth){
		String sql = "update auth set icon = ? where id = ?";
		this.getJdbcTemplate().update(sql,auth.getIcon(),auth.getId());
	}
	public void updateAuthLv(Auth auth){
		String sql = "update auth set lv = ? where id = ?";
		this.getJdbcTemplate().update(sql,auth.getLv(),auth.getId());
	}

	public void updateAuthority(Auth auth) {
		String sql = "update auth set authority=? where id = ?";
		this.getJdbcTemplate().update(sql, auth.getAuthority(),auth.getId());
	}
	public void updateAuthFansLv(Auth auth) {
		String sql = "update auth set fans_lv=? where id = ?";
		this.getJdbcTemplate().update(sql, auth.getFansLv(),auth.getId());
	}

	@Override
	public void updateAuthGuizu(Auth auth) {
		String sql = "update auth set guizu=? where id=?";
		this.getJdbcTemplate().update(sql, auth.getGuizu(),auth.getId());
	}

	@Override
	public void updateAuthScore(Auth auth) {
		String sql = "update auth set score=? where id=?";
		this.getJdbcTemplate().update(sql, auth.getScore(),auth.getId());
	}


	public List<Auth> selectAuthListByAuthority(int authority) {
		String sql = "select * from auth where authority = ?";
		List<Auth> list = this.getJdbcTemplate().query(sql, new AuthRowMapper(),authority);
		return list;
	}

	public Auth selectByName(String name) {
		String sql = "select * from auth where name = ? order by authority desc";
		List<Auth> list = this.getJdbcTemplate().query(sql, new AuthRowMapper(),name);
		if (list.isEmpty()){
			return null;
		} else {
			return list.get(0);
		}
	}
	
	

	public List<Auth> selectByLikeName(String name) {
		String sql = "select * from auth where name like concat('%',?,'%')";
		List<Auth> list = this.getJdbcTemplate().query(sql, new AuthRowMapper(),name);
		return list;
	}



}
